<template>
	<div class="count">
		<h2>{{description}}</h2>
		<h2>当前求和为:{{sum}}</h2>
		<h2>当前求和乘10为:{{bigSum}}</h2>
		<select v-model.number="n">
			<option value="1">1</option>
			<option value="2">2</option>
			<option value="3">3</option>
		</select>
		<button @click="increment">加</button>
		<button @click="decrement">减</button>
		<button @click="jiaOdd(n)">当前和为奇数再加</button>
		<button @click="jiaWait(n)">等一等再加</button>
		<button @click="jiaAjax">联系一下服务器，看看加几</button>
	</div>
</template>

<script>
	import {mapState,mapGetters,mapActions} from 'vuex'

	export default {
		name:'Count',
		data() {
			return {
				n:1,
			}
		},
		methods: {
			//加法
			increment(){
				this.$store.commit('JIA',this.n)
			},
			//减法
			decrement(){
				this.$store.commit('JIAN',this.n)
			},

			/* ****************************************************************** */

			//和为奇数再加
			/* incrementIfOdd(){
				this.$store.dispatch('jiaOdd',this.n)
			}, */
			//等一等再加
			/* incrementWait(){
				this.$store.dispatch('jiaWait',this.n)
			}, */
			//联系服务器加
			/* incrementAjax(){
				this.$store.dispatch('jiaAjax')
			}, */

			/* （第一种写法，对象式）靠mapActions读取vuex中actions里的jiaOdd、jiaWait、jiaAjax。
			生成incrementIfOdd、incrementWait、incrementAjax */
			// ...mapActions({incrementIfOdd:'jiaOdd',incrementWait:'jiaWait',incrementAjax:'jiaAjax'}),

			/* （第一种写法，数组式）靠mapActions读取vuex中actions里的jiaOdd、jiaWait、jiaAjax。
			生成jiaOdd、jiaWait、jiaAjax */
			...mapActions(['jiaOdd','jiaWait','jiaAjax']),
			
		},
		computed: {
			...mapState(['description','sum']),
			...mapGetters(['bigSum'])
		}
	}
</script>

<style>
	.count{
		background-color: skyblue;
		padding: 20px;
	}
	select,button{
		height: 30px;
		margin-right: 10px;
	}
</style>